﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SMG.Domain.Repository;
using SMG.Domain.Repository.Program;
using SMG.ServiceContract.Program;
using SMG.Application.DTO.Program;

using SMG.Application.Extension;
using Infrastructure.Core.Common;
using SMG.Domain.Specifications;
using SMG.Domain.Specifications.Program;
using SMG.Domain.Model.Program;

namespace SMG.Application.Program
{
    /// <summary>
    /// 频道服务实现
    /// </summary>
    public class ChannelServiceImpl : ApplicationService,IChannelService
    {
        IChannelRepository channelRepository;
        IDeviceRepository deviceRepository;

        public ChannelServiceImpl(IRepositoryContext context, 
                                    IChannelRepository channelRepository,
                                    IDeviceRepository deviceRepository)
            : base(context)
        {
            this.channelRepository = channelRepository;
            this.deviceRepository = deviceRepository;
            this.channelRepository.SetContext(context);
            this.deviceRepository.SetContext(context);
        }

        /// <summary>
        /// 根据频道ID获取频道信息
        /// 这里的channelId并非主键id，而是频道ID
        /// </summary>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public ChannelDto GetChannelByChannelId(String channelId)
        {
            var channel = this.channelRepository.QueryBy(new ChannelCIdEqualSpecification(channelId)).FirstOrDefault();
            return channel.ConvertToDto();
        }

        /// <summary>
        /// 返回所有频道列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ChannelDto> QueryChannelAll()
        {
            return this.channelRepository.QueryAll().ToList().ConvertToDto();
        }

        /// <summary>
        /// 获取频道对应的设备列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DeviceDto> GetDeviceByChannel(String channelId) {
            var devices = new List<DeviceDto>();

            var channel = GetChannelByChannelId(channelId);
            if (channel == null) return null;

            var deviceIds = new List<String>();

            #region 获取频道的设备id列表

            if (!String.IsNullOrEmpty(channel.DeviceIdsMaster)) {
                var ids = channel.DeviceIdsMaster.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (ids != null && ids.Length > 0) {
                    foreach (var id in ids) {
                        if (!deviceIds.Contains(id)) deviceIds.Add(id);
                    }
                }
            }
            if (!String.IsNullOrEmpty(channel.DeviceIdsSlave))
            {
                var ids = channel.DeviceIdsSlave.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (ids != null && ids.Length > 0)
                {
                    foreach (var id in ids)
                    {
                        if (!deviceIds.Contains(id)) deviceIds.Add(id);
                    }
                }
            }

            #endregion

            if (!deviceIds.Any()) return null;

            deviceIds.ForEach(id =>
            {
                var device = deviceRepository.GetDeviceByID(id);
                if (device != null) {
                    devices.Add(device.ConvertToDto());
                }
            });

            return devices;
        }
    }
}
